package com.luzh.myspring.context.support;

import com.luzh.myspring.beans.BeanException;
import com.luzh.myspring.beans.factory.ConfigurableListableBeanFactory;
import com.luzh.myspring.beans.factory.support.DefaultListableBeanFactory;

/**
 * 可刷新容器的应用上下文
 */
public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {

    private DefaultListableBeanFactory beanFactory;

    /**
     * 重新创建容器并加载bean定义
     * @throws BeanException
     */
    @Override
    protected void refreshBeanFactory() throws BeanException {
        this.beanFactory = createBeanFactory();
        loadBeanDefinitions(beanFactory);
    }

    /**
     * 创建容器
     * @return
     */
    private DefaultListableBeanFactory createBeanFactory() {
        return new DefaultListableBeanFactory();
    }

    @Override
    protected ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }

    protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory);

}
